/**
 * 渠道产品映射
 * @author linfeng
 * @since 2019/11/11
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-form");

    form.verify({
        productId: function (value) {
            if (!zmnUtil.isEmptyNull(value)) {
                if (value.length > 49) {
                    return '渠道方产品父ID长度需要小于50个字符';
                }
            }
        }
    });
    zmnLayUtil.layFormVerify(form);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let channelProductSubId = obj.field.channelProductSubId;
        if (channelProductSubId != null && channelProductSubId.length > 1000){
            layer.msg("渠道方产品子ID长度超过1000了，请调整后再试", {icon: 2});
            return false;
        }

        if (!zmnUtil.isEmptyNull(channelProductSubId)){
            var idNum = channelProductSubId.split(",");
            if (idNum.length > 20){
                layer.msg("渠道方产品子ID最多只能配置20个，请调整后再试", {icon: 2});
                return false;
            }
        }

        var openId = layer.open({type: 3});
        var url = "/channel/product/map/addChannelProductMapping.action";

        let data = {
            productId: json.productId,
            channelId: json.channelId,
            relateId: obj.field.relateId,
            channelProductParentId: obj.field.channelProductParentId,
            channelProductSubId: obj.field.channelProductSubId
        };

        $.ajax({
            async: false,
            type: "POST", url: url,
            data: data,
            success:function(result) {
                layer.close(openId);
                if(result.status == 200) {
                    zmnLayUtil.closeThisIframeMsg(parent, result.message, "zmn-table");
                    parent.location.reload();
                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })

        return false;
    });

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });


    //对外暴露的接口
    exports('channel_servproduct_mappingChannelProduct', {});
});